package org.ww.edu.controller;


import com.sun.tracing.dtrace.ProviderAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.ww.commonutils.R;
import org.ww.edu.bean.Video;
import org.ww.edu.bean.vo.VideoVo;
import org.ww.edu.client.VodClient;
import org.ww.edu.service.VideoService;
import org.ww.servicebase.exceptionhandler.GuliException;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-10-23
 */
@RestController
@RequestMapping("/edu/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private VodClient vodClient;

    /**
     * 添加小节信息
     * @param video
     * @return
     */
    @PostMapping("addVideo")
    public R addVideo(@RequestBody Video video){
        videoService.save(video);
        return R.ok();
    }

    /**
     * 删除小节信息
     * TODO: 后边需要完完善：删除小节的同时删除视频
     */
    @DeleteMapping("/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        //根据videoId获取视频id
        Video video = videoService.getById(videoId);
        String vodId=video.getVideoSourceId();
        //删除视频
        if(!StringUtils.isEmpty(vodId)){
            R result = vodClient.removeVod(vodId);
            if(result.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，熔断器...");
            }
        }

        videoService.removeById(videoId);
        return R.ok();
    }

    /**
     * 修改小节信息
     */
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody Video video){
        videoService.updateById(video);
        return R.ok();
    }

    /**
     * 根据小节id获取小节信息
     */
    @GetMapping("/{videoId}")
    public R getVideoInfo(@PathVariable String videoId){
        Video video = videoService.getById(videoId);
        return R.ok().data("videoInfo",video);
    }
}

